<?php
namespace m\controllers;

use Yii;
use yii\db\Query;
use common\models\Category;
use common\models\Product;
use common\models\ProductCategory;
use common\models\Post;
use common\models\Brand;
use common\models\Banner;
use common\models\Order;
use yii\helpers\ArrayHelper;

/**
 * Index controller
 */
class IndexController extends MController
{

    public $enableCsrfValidation = false;
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        //修改页面标题
        Yii::$app->view->params['page']['page_title'] = '首页 - ' . $this->w_config['website_name'];

        $cache = Yii::$app->cache;

    	//增加点击量
        $db = Yii::$app->db;
        $co = Yii::$app->request->post('name');
        $link = Yii::$app->request->post('link');

        if($co && $link){
            $sql = "UPDATE `banner` SET `click_count` += 1 where `id` = '$co'";
            $db->createCommand($sql)->execute();
            return $link;
        }
         //查询轮播广告
        $ban = $cache->get('banner');
        if ($ban === false) {
            $banner = Banner::find()
                    ->select('id,name,img_url,link,width,height,target')
                    ->where(['pid'=>1,'enabled'=>1])
                    ->limit(8)
                    ->asArray()
                    ->orderBy('orderBy asc')
                    ->all();
                    //$banne = array ();
            foreach ($banner as $k => $value)
            {
                $ban[] = array (
                    'id' => $value['id'],
                    'name' => $value['name'],
                    'link' => $value['link'],
                    'img_url' => $value['img_url'],
                    'width' => $value['width'],
                    'height' => $value['height'],
                    'target' => $value['target'],
                );
            }
            $cache->set('banner',$ban);
        }
        $query = (new Query())
                ->select('a.id,a.name,a.img_url,a.product_price,a.price_unit,a.spec,b.name bname')
                ->from('product AS a')
                ->where(['a.is_show'=>1])
                ->leftJoin('brand AS b','a.brand_id = b.id')
                ->orderBy('a.hot_sort,a.sort_numer,a.id');
        //热门商品
        $pro_list = $query->limit(10)->all();
        $pro_list = $this->sel_order($pro_list);
        
        $load = Yii::$app->request->post('offset');

        if($load){
            $per = 5; //每次加载的数量
            $pro_list = $query->limit($per)->offset(($load+1)*$per)->all();
            $pro_list = $this->sel_order($pro_list);

            return json_encode($pro_list);
        }

        return $this->render('index',[
        	'banner' => $ban,
        	'pro_list' =>$pro_list,
        ]);
    }

    function sel_order($list){
        foreach ($list as $k => $v) {
            //已售
            $sale = (new Query())
                    ->select('sum(b.product_num)')
                    ->from('order_product AS a')
                    ->where(['a.product_id'=>$v['id'],'b.order_status'=>4,'b.pay_status'=>2,'b.shipping_status'=>3])
                    ->leftJoin('order AS b','b.id = a.order_id')
                    ->scalar();
            $list[$k]['sale_sum'] = $sale?$sale:0;
        }
        return $list;
    }

    //关于我们
     public function actionAbout()
    {
        //修改页面标题
        Yii::$app->view->params['page']['page_title'] = '关于我们 - ' . $this->w_config['website_name'];
        
        $id=array('26','28','29','30');
        $post = Post::find()
            ->where(['in','id',$id])
            ->all();
        $article = Post::find()
            ->where(['id'=>26])
            ->One();
         
        return $this->render('about',[
            'post'=>$post,
            'article'=>$article,
        ]);
    }
    
  
}

